﻿/// <summary>
/// Author: HBLAi
/// Date: 2013/06/07
/// </summary>
using System;
using System.Web.Mvc;
using SketchTour.Models;
using Ivs.Core.Mapping;
using SketchTour.DAL;
using SketchTour.Paging;
using SketchTour.Common;
using Ivs.Core.Common.Utilities;
using System.Collections;
using System.Collections.Generic;

namespace SketchTour.Controllers.Master
{
    public class MSST00Controller : BaseController
    {
        int RecordsPerPage = Utilities.GetNumberPerPage();
       
        Dictionary<string, string> Sex = new Dictionary<string, string>()
	    {
	        {"Male", "Male"},
	        {"FeMale", "FeMale"}
	    };

        private MSST00DAL _service;

        public MSST00Controller()
        {
            _service = new MSST00DAL();
        }

        public ActionResult Index()
        {
            MSST00Models Model = null;
            //xoa thong tin session tu cac trang khac
            if (Request.UrlReferrer != null)
            {
                if (Request.Url.ToString() != Request.UrlReferrer.ToString())
                {
                    Session.Remove("SEARCH_CONDITION");
                }
                else
                {
                    Hashtable htbCondition = (Hashtable)Session["SEARCH_CONDITION"];
                    //Neu da co dieu kien search thi search lai du lieu
                    if (!Utilities.isNullOrEmpty(htbCondition) && !Utilities.isNullOrEmpty(htbCondition[CData.ScreenID.MSST00]))
                    {
                        Model = (MSST00Models)htbCondition[CData.ScreenID.MSST00];
                    }
                }
            }
            else
            {
                Session.Remove("SEARCH_CONDITION");
            }

            //search du lieu neu Model khong rong
            IEnumerable<MSST00Models> data = new List<MSST00Models>();
            if (!Utilities.isNullOrEmpty(Model))
            {
                int result = _service.Search(Model, out data);
            }
            var list = data.ToPagedList(1, RecordsPerPage);
            //
            ViewBag.StaffTypeID = new SelectList(_service.GetStaffType(), MSST00Key.StaffTypeID, MSST00Key.StaffType);
            ViewBag.Sex = new SelectList(Sex, "Key", "Value");
            return View(list);
        }
        [HttpPost]
        public ActionResult Index(int? page, MSST00Models Model)
        {
            //Kiem tra session luu dieu kien search
            if (!Utilities.isNullOrEmpty(Session["SEARCH_CONDITION"]))
            {
                Hashtable htbCondition = (Hashtable)Session["SEARCH_CONDITION"];
                //khi phan trang se su dung lai dieu kien search
                //khi page.HasValue la su dung phan trang
                if (!Utilities.isNullOrEmpty(htbCondition[CData.ScreenID.MSST00]) && page.HasValue)
                {
                    Model = (MSST00Models)htbCondition[CData.ScreenID.MSST00];
                }
                //khi search, page = null
                //luu lai dieu kien search moi
                else
                {
                    htbCondition[CData.ScreenID.MSST00] = Model;
                    Session["SEARCH_CONDITION"] = htbCondition;
                }

            }
            //luu lai dieu kien search moi
            else
            {
                Hashtable htbCondition = new Hashtable();
                htbCondition[CData.ScreenID.MSST00] = Model;
                Session["SEARCH_CONDITION"] = htbCondition;
            }

            IEnumerable<MSST00Models> data;
            int result = _service.Search(Model, out data);
            ViewBag.StaffTypeID = new SelectList(_service.GetStaffType(), MSST00Key.StaffTypeID, MSST00Key.StaffType);
            ViewBag.Sex = new SelectList(Sex, "Key", "Value");
            int pageSize = RecordsPerPage;
            int pageNumber = (page ?? 1);
            var list = data.ToPagedList(pageNumber, pageSize);
            return PartialView("PVStaffList", list);
        }

        //
        // MSST00/Add
        public ActionResult Add()
        {
            ViewBag.StaffTypeID = new SelectList(_service.GetStaffType(), MSST00Key.StaffTypeID, MSST00Key.StaffType);
            ViewBag.Sex = new SelectList(Sex, "Key", "Value");
            return View();
        }

        //
        // POST: /MSST00/Add

        [HttpPost]
        public ActionResult Add(MSST00Models StaffModel)
        {
            if (ModelState.IsValid)
            {
                int Results = _service.Insert(StaffModel);
                TempData[CData.MsgContent] = CMessage.DisplayMessageInserted(Results);

                return RedirectToAction("Index");
            }
            ViewBag.StaffTypeID = new SelectList(_service.GetStaffType(), MSST00Key.StaffTypeID, MSST00Key.StaffType);
            ViewBag.Sex = new SelectList(Sex, "Key", "Value");
            return View(StaffModel);
        }

        //
        // GET: /MSST00/Edit/5

        public ActionResult Edit(string idEdit)
        {
            string[] str = idEdit.Split(';');
            string[] s = new string[str.Length];
            for (int i = 0; i < str.Length; i++)
            {
                s[i] = str[i].Replace("chkPODetail", string.Empty);
            }
            string id = s[0];
            if (s.Length != 0)
            {
                MSST00Models obj = _service.Search(id);
                if (obj == null)
                {
                    return HttpNotFound();
                }
                ViewBag.StaffTypeID = new SelectList(_service.GetStaffType(), MSST00Key.StaffTypeID, MSST00Key.StaffType, obj.StaffTypeID);
                ViewBag.Sex = new SelectList(Sex, "Key", "Value", obj.Sex);
                return View(obj);
            }
            return RedirectToAction("Index");
        }

        //
        // POST: /MSST00/Edit

        [HttpPost]
        public ActionResult Edit(MSST00Models StaffModel)
        {
            if (ModelState.IsValid)
            {
                int Results = _service.Update(StaffModel);
                TempData[CData.MsgContent] = CMessage.DisplayMessageUpdated(Results);

                return RedirectToAction("Index");
            }
            ViewBag.StaffTypeID = new SelectList(_service.GetStaffType(), MSST00Key.StaffTypeID, MSST00Key.StaffType);
            ViewBag.Sex = new SelectList(Sex, "Key", "Value");
            return View(StaffModel);
        }

        public ActionResult Delete(string idDelete)
        {
            string[] str = idDelete.Split(';');
            string[] s = new string[str.Length];
            for (int i = 0; i < str.Length; i++)
            {
                s[i] = str[i].Replace("chkPODetail", string.Empty);
            }
            if (s.Length != 0)
            {
                foreach (var value in s)
                {
                    if (!String.IsNullOrEmpty(value))
                    {
                        int Results = _service.Delete(value);
                        TempData[CData.MsgContent] = CMessage.DisplayMessageDeleted(Results);
                    }
                }
                return RedirectToAction("Index");

            }
            return RedirectToAction("Index");
        }
    }
}
